﻿using BenchmarkDotNet.Attributes;
using KdbndpTest.Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KdbndpTest.BenchmarkRunner
{
    public class Demo4_DeleteableRunner
    {

        [Benchmark(Description = "正常删除单条数据")]
        public void Delet()
        {
            var db = GetInstance();

            var insertObj = new Order() { Name = "order1", Price = 0 };

            //Ignore  CreateTime
            var insertId = db.Insertable(insertObj).IgnoreColumns(it => new { it.CreateTime }).ExecuteReturnIdentity();//get identity

            //by entity
            db.Deleteable<Order>().Where(new Order() { Id = insertId }).ExecuteCommand();
        }

        [Benchmark(Description = "删除列表")]
        public void DeleteList()
        {
            var db = GetInstance();

            var insertObj = new Order() { Name = "order1", Price = 0 };

            //Ignore  CreateTime
            var insertId = db.Insertable(insertObj).IgnoreColumns(it => new { it.CreateTime }).ExecuteReturnIdentity();//get identity

            //by entity
            //by primary key
            db.Deleteable<Order>().In(insertId).ExecuteCommand();

            //by primary key array
            //db.Deleteable<Order>().In(new int[] { 1111, 2222 }).ExecuteCommand();
        }

        [Benchmark(Description = "表达式删除")]
        public void DeleteExpression()
        {
            var db = GetInstance();

            var insertObj = new Order() { Name = "order1", Price = 0 };

            //Ignore  CreateTime
            var insertId = db.Insertable(insertObj).IgnoreColumns(it => new { it.CreateTime }).ExecuteReturnIdentity();//get identity

            db.Deleteable<Order>().Where(it => it.Id == insertId).ExecuteCommand();
        }


        private static SqlSugarClient GetInstance()
        {
            var db = new SqlSugarClient(new ConnectionConfig()
            {
                DbType = SqlSugar.DbType.Kdbndp,
                ConnectionString = Config.ConnectionString,
                InitKeyType = InitKeyType.Attribute,
                IsAutoCloseConnection = true,
                MoreSettings = new ConnMoreSettings()
                {
                    DatabaseModel = DbType.PostgreSQL
                },
                AopEvents = new AopEvents
                {
                    OnLogExecuting = (sql, p) =>
                    {
                        Console.WriteLine(sql);
                        Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
                    }
                }
            });

            //If no exist create datebase 
            db.DbMaintenance.CreateDatabase();

            //Use db query
            var dt = db.Ado.GetDataTable("select 1");

            //Create tables
            db.CodeFirst.InitTables(typeof(OrderItem), typeof(Order));

            return db;
        }

    }
}
